<?php
namespace app\admin\dao;

use xto\data\Sql;
use xto\App;
use xto\Util;
use xto\membership\context\Context;

use xto\data\IDao;

class MessageDao extends \xto\data\dao\MessageDao{
	public function getinbox($pagesize){
		$user=Context::current()->getManager(); 
		$this->setorder('mbid','desc');
		return $this->get_messager($pagesize,'',$user->username);
	} 

	public static function update($data = [], $where = [], $field = NULL){
		$n=[
			'mbid'=>$data['mbid'],
			'isread'=>false, 
			'accepter'=>$data['accepter'],
			'sender'=>$data['sender'],

			'updatetime'=>Util::getdate()
		];
		$status=parent::update($n);
		$d=[
			'mid' 	=>$data['mid'],
			'title'=>$data['title'],
			'content'=>$data['content'],
			'remsg' =>$data['remsg'],
		];
		Sql::update('xto_message_data',$d);
		return $status;
	}

	public function user_read($data){
		$n=[
			'mbid'=>$data['mbid'],
			'isread'=>true,
			'updatetime'=>Util::getdate()
		];
		parent::update($n);
		$d=[
			'mid' 	=>$data['mid'],
			'remsg' =>$data['remsg'],
		];
		return Sql::update('xto_message_data',$d);
	}

	public function getsendbox($pagesize){
		$user=Context::current()->getManager(); 
		$this->setorder('mbid','desc');
		return $this->get_messager($pagesize,$user->username,'');
	} 

	public function detail($mbid){
		$user=Context::current()->getManager(); 
		$this->map['sender']=$user->username;
		return $this->find($mbid);
	}

	public function read($mbid){
		$user=Context::current()->getManager(); 
		$this->map['accepter']=$user->username;

		$data=[
			'mbid'=>$mbid,
			'isread'=>true,
			'accepter'=>$user->username
		];
		Sql::update($this->_tablename,$data);
		return $this->find($mbid);
	}
}